<template>
    <div class="h-100">
        <iframe
            :src="viewUrl"
            class="iframe"
            id="service_iframe"
            ref="iframeDom"
            :style="{ height: height }"
        ></iframe>

        <com-formDialog
            ref="dialog"
            v-bind="dialogData"
            @cancel="dialogCancelHandle"
            @submit="submitHandle"
        />
        <div id="showSuccess">
            <div id="tipSuccess" class="modal modal_middle hide fade" tabindex="-1" data-toggle="modal" data-target="#tipSuccess" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true">
                <div id="tip_msg"></div>
            </div>
        </div>
        <!-- 主管验证 -->
        <div id="fingerDirectorCheck" @click="fingerDirectorCheck"></div>
        <div id="billGetPModal"></div>
    </div>
</template>

<script>
import { MOUDLE_MAP } from "@constants";
import bindWindowEventMixin from "./bindWindowEvent";

const getIsInnerConnectError = (path) => {
    // if (path.indexOf(addInnerFrontBase) === 0) {
    //     if (!sessionStorage.getItem("navTree")) {
    //         return true;
    //     }
    // }

    return false;
};

const getIsIvrConnectError = (path) => {
    // if (path.indexOf(addIvrFrontBase) === 0) {
    //     if (!sessionStorage.getItem("ivrInitAuth")) {
    //         return true;
    //     }
    // }

    return false;
};

const getIsCsmsConnectError = (path) => {
    // if (path.indexOf(addCsmsFrontBase) === 0) {
    //     if (!sessionStorage.getItem("csmsInitAuth")) {
    //         return true;
    //     }
    // }

    return false;
};

export default {
    mixins: [bindWindowEventMixin],
    props: {
        path: String,
        height: { type: String, default: "100%" },
    },
    data() {
        let { path } = this.$route.query;

        path = this.path || path;

        return {
            showInnerConnectError: getIsInnerConnectError(path),
            showCsmsConnectError: getIsCsmsConnectError(path),
            showIvrConnectError: getIsIvrConnectError(path),
            viewUrl: path,
            comDialogData: {},
            loading: true,
            iframeLoading: true
        };
    },
    computed: {
        dialogData() {
            let { comDialogData } = this;
            let computedConfigs = _.get(this, `${comDialogData.configsName}`);
            return {
                configs: computedConfigs,
                ...comDialogData,
            };
        },
    },
    watch: {
        // path(value, oldValue) {
        //     if (value === oldValue) {
        //         return;
        //     }
        //     this.showInnerConnectError = getIsInnerConnectError(value);
        //     this.showCsmsConnectError = getIsCsmsConnectError(value);
        //     this.showIvrConnectError = getIsIvrConnectError(value);
        // },
    },
    methods: {
        dialogCancelHandle() {
            this.$refs.dialog.close();
        },
        async submitHandle(data) {
            if (typeof this.dialogData.submitHandle === "string") {
                let handle = this[this.dialogData.submitHandle];
                if (handle) {
                    let loading = await this.$store.dispatch("global/loading");
                    let result = await handle(data).catch((e) => {
                        loading.close();
                    });
                    loading.close();
                    this.$refs.dialog.close();
                }
            } else if (typeof this.dialogData.submitHandle === "function") {
                this.dialogData.submitHandle(data);
            }
        },
        iframeOnload() {
            let serviceIframe = this.$refs.iframeDom;
            serviceIframe.onload = () => {
                setTimeout(() => {
                    this.loading = false;
                    this.iframeLoading = false;
                });
                this.cmsPageHandle(serviceIframe);
            };
        },
       
    },
    mounted() {
        this.$nextTick(() => {
            this.$refs.iframeDom.contentWindow.onunload = (e) => {
                let isCsmsNoAuthPageSign = false;

                Array.prototype.forEach.call(
                    this.$refs.iframeDom.contentWindow.document.getElementsByTagName(
                        "script"
                    ),
                    (scriptDom) => {
                        if (
                            scriptDom.innerHTML.indexOf(
                                `        alert("你未登录或者已被强制注销登录，请重新登录");
        location.href = "/` // (CSMSClientTest|IVRClientTest)/adminAction_login.do";
                            ) !== -1
                        ) {
                            isCsmsNoAuthPageSign = true;
                        }
                    }
                );

                if (isCsmsNoAuthPageSign) {
                    this.showCsmsConnectError = true;
                    this.showIvrConnectError = true;
                }
            };
        });

        this.bindWindowEvent();
        this.iframeOnload();
    },
    destroyed() {
        
    },
};
</script>

<style lang="scss" scoped>
.iframe {
    width: 100%;
    height: 100%;
    outline: none;
    border: none;
}
.hide {
    display: none;
}
</style>
